package com.hj.dapp.business.user.model;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.hj.dapp.business.role.model.Role;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static com.baomidou.mybatisplus.annotation.FieldStrategy.NOT_EMPTY;

/**
 * @author 胡奥程
 * @version 1.0
 * @date 2023/12/29
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Schema(title = "用户模型", description = "user")
@TableName("user")
public class User implements UserDetails {
    @TableId(type = IdType.AUTO)
    @Schema(name = "ID", description = "用户ID属性", format = "int64", example = "1")
    private Long id;
    @Schema(name = "用户名", example = "huaocheng")
    private String username;

    @Schema(name = "密码", example = "123456")
    private String password;
    @Schema(name = "邮箱", example = "18923099907@163.com")
    private String email;
    @Schema(name = "手机号", example = "18923099907")
    private String phone;
    @Schema(name = "是否启用状态",format = "int64", example = "1")
    private Integer status;
    @Schema(name = "部门id", example = "1")
    private Long deptId;
    @Schema(name = "创建人", example = "huaocheng")
    private String createBy;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    @Schema(name = "创建时间",type = "dateTime", example = "2023-12-30 05:00:12")
    private LocalDateTime createTime;
    @Schema(name = "更新人", example = "jiawenbo")
    private String lastUpdateBy;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    @Schema(name = "更新时间",type = "dateTime", example = "2023-12-30 05:00:12")
    private LocalDateTime lastUpdateTime;
    @Schema(name = "删除标志",format = "int64", example = "0")
    @TableField(value = "del_flag",updateStrategy =NOT_EMPTY )
    private Integer delFlag;
    @Schema(name = "角色列表",format = "array")
    @TableField(exist = false)
    private List<Role> roles;
    @Schema(name = "性别",format = "int64", example = "1")
    private Integer gender;
    @Schema(name = "头像")
    private String avatar;
    @Schema(name = "真实姓名")
    private String realName;


    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return roles.stream().map(n -> new SimpleGrantedAuthority("ROLE_" + n.getExpr())).collect(Collectors.toList());
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
